//
// Created by Administrator on 2020/10/21.
//

#include "../../../lib/csp/include/cspApi.h"
#include "../../ccore/include/commDef.h"
#include "../../ccore/misc/include/misc.h"
#include "../include/memPools.h"

#ifdef O5G_TEST
static S_MemPoolDesc memPoolDescTable4Amf[] =
{
    { MP_SIZE(128), MP_COUNT(64)},
    { MP_SIZE(256), MP_COUNT(64)},
    { MP_SIZE(512), MP_COUNT(32)},
    { MP_SIZE(1024), MP_COUNT(32)},
    { MP_SIZE(2048), MP_COUNT(16)},
    { MP_SIZE(4096), MP_COUNT(16)},
    { MP_SIZE(8192), MP_COUNT(8)},
    { MP_SIZE(16384), MP_COUNT(8)}
};

static S_MemPoolDesc memPoolDescTable4Upf[] =
{
    { MP_SIZE(128), MP_COUNT(64)},
    { MP_SIZE(256), MP_COUNT(64)},
    { MP_SIZE(512), MP_COUNT(32)},
    { MP_SIZE(1024), MP_COUNT(32)},
    { MP_SIZE(2048), MP_COUNT(16)},
    { MP_SIZE(4096), MP_COUNT(16)},
    { MP_SIZE(8192), MP_COUNT(8)},
    { MP_SIZE(16384), MP_COUNT(8)}
};
#else
static S_MemPoolDesc memPoolDescTable4Amf[] =
{
    { MP_SIZE(128), MP_COUNT(10240)},
    { MP_SIZE(256), MP_COUNT(8192)},
    { MP_SIZE(512), MP_COUNT(8192)},
    { MP_SIZE(1024), MP_COUNT(4096)},
    { MP_SIZE(2048), MP_COUNT(2048)},
    { MP_SIZE(4096), MP_COUNT(1024)},
    { MP_SIZE(8192), MP_COUNT(1024)},
    { MP_SIZE(16384), MP_COUNT(256)}
};

static S_MemPoolDesc memPoolDescTable4Upf[] =
{
    { MP_SIZE(128), MP_COUNT(10240)},
    { MP_SIZE(256), MP_COUNT(8192)},
    { MP_SIZE(512), MP_COUNT(8192)},
    { MP_SIZE(1024), MP_COUNT(4096)},
    { MP_SIZE(2048), MP_COUNT(2048)},
    { MP_SIZE(4096), MP_COUNT(1024)},
    { MP_SIZE(8192), MP_COUNT(1024)},
    { MP_SIZE(16384), MP_COUNT(256)}
};
#endif

static uint32_t gMemPoolMode[] = 
{
    MP_MODE_POWER2, MP_MODE_POWER2
};
STATIC_ASSERT(ARRAY_SIZE(gMemPoolMode) == CN_MP_TYPE_MAX);

static S_MemPoolDesc* gMemPoolDesc[] =
{
    memPoolDescTable4Amf, memPoolDescTable4Upf
};
STATIC_ASSERT(ARRAY_SIZE(gMemPoolDesc) == CN_MP_TYPE_MAX);

static uint32_t gMemPoolDescSize[] =
{
    ARRAY_SIZE(memPoolDescTable4Amf), ARRAY_SIZE(memPoolDescTable4Upf)
};
STATIC_ASSERT(ARRAY_SIZE(gMemPoolDescSize) == CN_MP_TYPE_MAX);

int32_t CN_MP_Init(void)
{
    int32_t rc;
    uint32_t loop;

    for (loop = 0; loop < CN_MP_TYPE_MAX; loop++)
    {
        rc = MPS_SetAttr(loop, gMemPoolMode[loop]);
        IF_RETURN(!=, O5G_RC_OK, rc, O5G_RC_ER, "CN_MP_Init() MPS_SetAttr(%u) error!\n", loop);
        rc = MPS_AddPools(loop, gMemPoolDesc[loop], gMemPoolDescSize[loop]);
        IF_RETURN(!=, O5G_RC_OK, rc, O5G_RC_ER, "CN_MP_Init() MPS_AddPools(%u) error!\n", loop);
        rc = MPS_Check(loop);
        IF_RETURN(!=, O5G_RC_OK, rc, O5G_RC_ER, "CN_MP_Init() MPS_Check(%u) error!\n", loop);
    }

    return O5G_RC_OK;
}

int32_t CN_MP_Final(void)
{
    MPS_Final();

    return O5G_RC_OK;
}

